package com.android.presenter.info;

import android.content.Context;

import com.android.model.net.HttpObserver;
import com.android.model.entity.res.AccountTokenResult;
import com.android.model.info.LocalSp;
import com.android.model.info.UserModel;
import com.android.presenter.base.BasePresenter;
import com.android.presenter.iview.IAccountTokenView;
import com.android.util.log.LogUtil;

/**
 * author:  TenFace
 * date:    2017/9/27
 * description: 创建用户的 Token
 */

public class AccountTokenPresenter extends BasePresenter<IAccountTokenView> {

    private static final String TAG = AccountTokenPresenter.class.getSimpleName();
    private UserModel mUserModel;

    public AccountTokenPresenter(IAccountTokenView iAccountTokenView) {
        super(iAccountTokenView);
        mUserModel = UserModel.getInstance();
    }

    public void createAccountToken(final Context context,
                                   String ssoToken) {

        mUserModel.createAccountToken(context,
                ssoToken,
                new HttpObserver<AccountTokenResult>() {
                    @Override
                    public void onSuccess(AccountTokenResult accountTokenResult) {
                        LogUtil.e(TAG, "成功！");
                        if (mIView != null) {
                            mIView.createAccountTokenSuccess(accountTokenResult);
                            LocalSp.getInstance(context).saveAccountToken(accountTokenResult.getValue());
                        }
                    }

                    @Override
                    public void onError(int errType, String errMessage) {
                        LogUtil.e(TAG, "失败码：" + errType + "》》信息：" + errMessage);
                        if (mIView != null) {
                            mIView.createAccountTokenFail(errType, errMessage);
                        }
                    }
                }, mIView.getLifeSubject());
    }

}
